package com.example.service;

import com.example.bean.Equipment;
import com.example.mapper.EquipmentMapper;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EquipmentService {

    @Resource
    EquipmentMapper mapper;

//    public PageInfo<Equipment> queryEquipment(Integer pageNum, Integer PageSize) {
//        //开启分页
//        PageHelper.startPage(pageNum, PageSize);
//        List<Equipment> equipment = mapper.queryEquipment();
//        return new PageInfo<>(equipment);
//    }

    public List<Equipment> selectTime() {
        return mapper.selectTime();
    }

    public List<Equipment> selectByName(String equipmentName) {
        equipmentName = "%" + equipmentName + "%";
        return mapper.selectByName(equipmentName);
    }

    public List<Equipment> selectByUserId(int userId) {
        return mapper.selectByUserId(userId);
    }

    public List<Equipment> getRepair() {
        return mapper.selectByState(2);
    }

    public void addEquipment(Equipment equipment) {
        equipment.setUserId(0);
        mapper.addEquipment(equipment);
    }

    public boolean deleteEquipment(int equipmentId) {
        Equipment equipment = mapper.selectById(equipmentId);
        if (equipment.getEquipmentState() != 0) return false; //如果被使用了不能删除
        mapper.deleteEquipment(equipmentId);
        return true;
    }

    public int repairEquipment(int equipmentId, int userId) {
        return mapper.updateEquipment(2, equipmentId, userId);
    }

    public int rentEquipment(int equipmentId, int userId) {
        return mapper.updateEquipment(1, equipmentId, userId);
    }

    public int recoveryEquipment(int equipmentId, int userId) {
        return mapper.updateEquipment(0, equipmentId, userId);
    }

    public List<Equipment> chargeEquipment(int equipmentId) {
        return mapper.chargeEquipment(equipmentId);
    }

}
